using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using System.IO;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class PhotoAddEdit : AdminBaseUserControl
    {
        public event EventHandler EventSaveSuccess;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCategory();
            }
        }


        public void DoEdit(object sender, EventArgs e)
        {
            try
            {
                PhotoList pList = (PhotoList)sender;
                
                BindEditPhoto(Convert.ToInt64(pList.SelectedID));
            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);
            }
        }

        public void DoEditRoot(object sender, EventArgs e)
        {
            try
            {
                CategoryTree c = (CategoryTree)sender;

                BindEditPhoto(Convert.ToInt64(c.SelectedValue));
            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);
            }
        }

        private void BindEditPhoto(long ID)
        {
            IContentCategory category = CategoryController.GetCategory(ID);
            if (category != null)
            {
                fsExt.Visible = false;
                lblStatus.Text = "View Photo";
                ViewState["Category"] = category;
                tbName.Text = category.Name;
                tbDescription.Text = category.Description;
                ddlParent.SelectedValue = category.ParentID.ToString();
                tbOrder.Text = category.Order.ToString();
                ucCmsExtPropertiesForm.PropertyList = category.ExtPropertyList;
                imgPhoto.Visible = true;
                imgPhoto.ImageUrl = "../../ImageView.aspx?photo=" + category.ID + ".jpg&mdim=80";
                if (ucCmsExtPropertiesForm.Controls.Count > 0)
                    fsExt.Visible = true;                
            }
        }

        public void DoNew(object sender, EventArgs e)
        {
            NewPhoto();
        }

        private void NewPhoto()
        {
            try
            {
                BindCategory();
                lblStatus.Text = "Add New Photo";
                fsExt.Visible = false;
                ViewState["Category"] = null;
                tbName.Text = "";
                tbName.Focus();
                tbDescription.Text = "";
                tbOrder.Text = "0";
                tbName.ReadOnly = false;
                hdfCategoryID.Value = "0";
                imgPhoto.Visible=false;
                ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
                if (clazz != null)
                {
                    ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
                }
                if (ucCmsExtPropertiesForm.Controls.Count > 0)
                    fsExt.Visible = true;

            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);
            }
        }

        public void DoSaveMedia(object sender, EventArgs e)
        {
            long categoryID = 0;
            try
            {
                if (Session[SessionVariable.Clazz.CurrentID] != null)
                {
                    bool success = false;
                    IContentCategory category = new Category();
                    if (ViewState["Category"] != null)
                    {
                        category = (Category)ViewState["Category"];
                        category.EditedBy = IGlobalistMembership.GetUser().ID;
                        categoryID = category.ID;
                    }
                    category.ExtPropertyList = ucCmsExtPropertiesForm.PropertyList;
                    category.Class = ClassController.GetClass(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
                    category.ClassID = Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]);
                    category.ClassTypeID = Convert.ToInt16(CmsObjectType.ContentCategory);
                    category.Name = tbName.Text;
                    category.ParentID = Convert.ToInt64(ddlParent.SelectedValue);
                    category.Order = Convert.ToInt16(tbOrder.Text);
                    category.Description = tbDescription.Text;
                    category.Author = IGlobalistMembership.GetUser().ID;
                    Session["CategoryID"] = Convert.ToInt64(ddlParent.SelectedValue);

                    if (ViewState["Category"] != null)
                    {
                        CategoryController.UpdateCategory(category);
                        success = true;
                    }
                    else
                    {
                        categoryID = CategoryController.InsertCategory(category);
                        success = true;
                    }


                    if ((EventSaveSuccess != null) && (success == true))
                    {
                        EventSaveSuccess(this, new EventArgs());
                    }
                    else
                    {
                        tbName.Focus();
                    }

                    //upload file
                    try
                    {
                        if (fuFile.HasFile)
                        {
                            string path = Path.Combine(DataRootPhysicalPath, "Photos");
                            HttpPostedFile fPost = fuFile.PostedFile;
                            string fName = fuFile.FileName;
                            string fExtention = Path.GetExtension(fPost.FileName);
                            fPost.SaveAs(Path.Combine(path, categoryID + ".jpg"));
                        }
                    }
                    catch { }

                }

            }
            catch (Exception ex)
            {
                Logger.Error("[[CategoryAddEdit]]", ex);
            }
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
        }

        public void BindCategory()
        {
            if (Session[SessionVariable.Clazz.CurrentID] != null)
            {
                IContentCategory category = CategoryController.GetCategoryTreeByClassID(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
                ddlParent.Items.Clear();
                ddlParent.Items.Add(new ListItem("Root", "0"));
                foreach (IContentCategory c in category.CategoryList)
                {
                    ddlParent.Items.Add(new ListItem("---" + c.Name.ToString(), c.ID.ToString()));
                }
            }
        }     
    }
}